import { createRouter,createWebHashHistory } from "vue-router";
import { Toast } from "vant";
const router = createRouter({
    history:createWebHashHistory(),
    routes:[
        {
            path:'/',
            component: () => import('../views/Home')
        },
      {
        path:'/home',
        component: () => import('../views/Home')
      },
      {
        path:'/order',
        component: () => import('../views/Order'),
        meta:{
          isAuth:true
        }
      },
      {
        path:'/mine',
        component: () => import('../views/Mine'),
        meta:{
          isAuth:true
        }
      },
      {
        path:'/cart',
        component: () => import('../views/Cart'),
        meta:{
          isAuth:true
        }
      },
      {
        path:'/store',
        component: () => import('../views/Store')
      },
      {
        path:'/createorder',
        component: () => import('../views/createOrder'),
        meta:{
          isAuth:true
        }
      },
      {
        path:'/address',
        component: () => import('../views/Address'),
        meta:{
          isAuth:true
        }
      },
      {
        path:'/addressedit',
        component: () => import('../views/AddressEdit'),
        meta:{
          isAuth:true
        }
      },
      {
        path:'/userinfoedit',
        component: () => import('../views/userinfoedit'),
        meta:{
          isAuth:true
        }
      },
      {
        path:'/login',
        component: () => import('../views/Login')
      },
      {
        path:'/register',
        component: () => import('../views/Register')
      },
    ]
})
//路由守卫
router.beforeEach((to,from,next) =>{
  if(to.meta.isAuth){
    //登录了以后，会在localstorage里面，存一个标识
    if(localStorage.isLogin === 'login'){
      next()
    }else{
      next('/login')
      Toast('请先去登录')
    }
  }else{
    next()
  }
})

export default router